$(function() {
	/**
	 * 查询FAQ框
	 */
	var lastTime = 0
	function getFAQ() {
		var href;
		var tab = $('#tab-menus').tabs('getSelected');
		var index = window.top.$('#tab-menus').tabs("getTabIndex", tab);
		var selectedPannel = $("#tab-menus .panel")[index];
		var ifr = $(selectedPannel).find("iframe");
		if (ifr.length == 0) {
			href = document.location.href;
		} else {
			var doc = ifr[0].contentWindow.document;
			var htmlTag = doc.getElementsByTagName('html');
			href = htmlTag[0].getAttribute('uri');
		}
		
		if (new Date().getTime() - lastTime < 3000) {
			_alert('点击速度过快，请稍侯重试！', false);
			return;
		}
		lastTime = new Date().getTime();

		// 从cookie中获取qq及qq群
		var cookieQQ;
		if (window.sessionStorage) {
			cookieQQ = window.sessionStorage.getItem("FAQ_QQ");
			if (cookieQQ) {
				cookieQQ = unescape(cookieQQ);
			}
		} else {
			cookieQQ = getCookie("FAQ_QQ");
		}

		var cookieQQGroup;
		if (window.sessionStorage) {
			cookieQQGroup = window.sessionStorage.getItem("FAQ_QQ_GROUP");
			if (cookieQQGroup) {
				cookieQQGroup = unescape(cookieQQGroup);
			}
		} else {
			cookieQQGroup = getCookie("FAQ_QQ_GROUP");
		}

		var cookieQQHit;
		if (window.sessionStorage) {
			cookieQQHit = window.sessionStorage.getItem("FAQ_QQ_HIT");
			if (cookieQQHit) {
				cookieQQHit = unescape(cookieQQHit);
			}
		} else {
			cookieQQHit = getCookie("FAQ_QQ_HIT");
		}

		var url = _FAQ_GET_URL;
		if (!href) {
			href = _FAQ_DEFAULT_URL;
		}
		var i = href.indexOf('?');
		if (i > 0) {
			href = href.substring(0, i);
		}
		var p = {};
		p.page = href;
		p.searchqq = (cookieQQHit == null ? true : false);
		p.searchqqgroup = (cookieQQGroup == null ? true : false);
		p.maxcommends = 20;

		$.ajax({
			url : url,
			type : 'post',
			dataType : 'json',
			data : p,
			success : function(data) {
				var commends = data.commends;
				if (commends) {
					var $faqHeaderTitle = $(".faqWin .faqHeader .faqTitle");
					var $faqCommends = $(".faqWin .faqHeader .faqCommends");
					$faqHeaderTitle.html('');
					$faqCommends.html('');
					if (commends.length > 0) {
						$faqCommends.animate({
							scrollTop : 0
						}, 'slow');
						var prefix = data.prefix;
						$faqHeaderTitle.html('您可能遇到的问题');
						var $ul = $("<ul></ul>");
						for ( var x in commends) {
							var commend = commends[x];
							var commendNum = commend.num;
							var commendName = commend.name;
							var $a = $("<a></a>");
							$a.attr('target', '_blank');
							$a.attr('href', prefix + "?relNum=" + commendNum);
							if (commendName.length > 40) {
								commendName = commendName.substring(0, 36)
										+ "...";

							}
							$a.html(commendName);

							var $commend = $("<li></li>");
							$commend.append($a);
							$ul.append($commend);
						}
						$faqCommends.append($ul);
					} else {
						$faqHeaderTitle.html('帮助咨询');
					}
				}

				var qqServers = new Array();
				var qqGroupServers = new Array();
				var servers = data.servers;
				if (servers && servers.length > 0) {
					for ( var x in servers) {
						var server = servers[x];
						if (server.serverType == '1') {
							qqServers.push(server);
						}
						if (server.serverType == '2') {
							qqGroupServers.push(server);
						}
					}
				}

				// 检查cookie中是否存在相应信息
				if (cookieQQHit != null) {
					qqServers = JSON.parse(cookieQQ);
				} else {
					// 保存qqservers
					if (window.sessionStorage) {
						window.sessionStorage.setItem("FAQ_QQ", JSON
								.stringify(qqServers));
					} else {
						document.cookie = "FAQ_QQ="
								+ escape(JSON.stringify(qqServers));
					}

				}
				if (cookieQQGroup != null) {
					qqGroupServers = JSON.parse(cookieQQGroup);
				} else {
					// 保存qqGroupServers
					if (window.sessionStorage) {
						window.sessionStorage.setItem("FAQ_QQ_GROUP", JSON
								.stringify(qqGroupServers));
					} else {
						document.cookie = "FAQ_QQ_GROUP="
								+ escape(JSON.stringify(qqGroupServers));
					}
				}

				if (qqServers.length > 0) {
					var qqServer = qqServers[0];

					$(".faqWin .faqBody .faqBody-qq").show();

					if (commends /* <![CDATA[ */&& /* ]]> */commends.length > 0) {
						$(".faqWin .faqBody .faqBody-content-1").html(
								'以上内容没有解决您的问题？');
						$(".faqWin .faqBody .faqBody-content-2").html(
								'您还可以咨询人工客服');
					} else {
						$(".faqWin .faqBody .faqBody-content-1").html('如有疑问');
						$(".faqWin .faqBody .faqBody-content-2").html(
								'您可咨询人工客服');
					}

					$(".faqWin .qq-contact").html('');

					var $serverImg = $("<img/>");
					$serverImg.attr('src', 'css/img/faq/QQ.png');
					$serverImg.attr('server-id', qqServer.id);
					$serverImg.attr('server-serverid', qqServer.serverId);
					$(".faqWin .qq-contact").append($serverImg);

					$(".faqWin .faqBody .qq-contacts .qq-contact-nickname")
							.html(qqServer.serverNickName);
					$(".faqWin .faqBody .qq-contacts .qq-contact-nickname")
							.attr('server-id', qqServer.id);
					$(".faqWin .faqBody .qq-contacts .qq-contact-nickname")
							.attr('server-serverid', qqServer.serverId);

					// 添加事件
					$(".faqWin .faqBody .qq-contacts .qq-contact-nickname")
							.unbind("click");
					$(".faqWin .faqBody .qq-contacts .qq-contact-nickname")
							.bind("click", function() {
								var id = $(this).attr("server-id");
								var serverid = $(this).attr("server-serverid");
								hitQQServer(id, serverid);
							});

					$serverImg.unbind("click");
					$serverImg.bind("click", function() {
						var id = $(this).attr("server-id");
						var serverid = $(this).attr("server-serverid");
						hitQQServer(id, serverid);
					});
				} else {
					$(".faqWin .faqBody .faqBody-qq").hide();
				}

				if (qqGroupServers.length > 0) {
					$(".faqWin .qq-group-contacts").html('');
					for ( var i in qqGroupServers) {
						var qqServer = qqGroupServers[i];
						var id = qqServer.id;
						var serverid = qqServer.serverId;
						var serverNickName = qqServer.serverNickName;

						var $qqGroupDiv = $("<a/>");
						$qqGroupDiv.attr('href', "javascript:void(0)");
						$qqGroupDiv.attr('server-id', id);
						$qqGroupDiv.attr('server-serverid', serverid);
						$qqGroupDiv.unbind("click");
						$qqGroupDiv.bind("click", function() {
							var id = $(this).attr("server-id");
							var serverid = $(this).attr("server-serverid");
							hitQQGroupServer(id, serverid);
						})
						$qqGroupDiv.html(serverNickName);
						$(".faqWin .qq-group-contacts").append($qqGroupDiv);
					}
				}
				showFAQ();
			},
			complete : function() {
			}
		});
	}

	function hitQQServer(id, serverid) {
		var url = 'http://wpa.qq.com/msgrd?v=3&uin=' + serverid
				+ '&site=qq&menu=yes';
		window.open(url);

		if (window.sessionStorage) {
			if (window.sessionStorage.getItem("FAQ_QQ_HIT") != null) {
				return;
			}
		} else {
			if (getCookie("FAQ_QQ_HIT") != null) {
				return;
			}
		}
		var p = {};
		p.id = id;
		$.ajax({
			url : _FAQ_HIT_URL,
			data : p,
			dataType : 'json',
			success : function(data) {
				if (data && data.code == 0) {
					if (window.sessionStorage) {
						window.sessionStorage.setItem("FAQ_QQ_HIT", "true");
					} else {
						document.cookie = "FAQ_QQ_HIT=true";
					}
				}
			},
			error : function() {
			},
			complete : function() {
			}
		});
	}

	function hitQQGroupServer(id, serverid) {
		window.open('http://shang.qq.com/wpa/qunwpa?idkey=' + serverid);

		if (window.sessionStorage) {
			if (window.sessionStorage.getItem("FAQ_QQGROUP_HIT_" + id) != null) {
				return;
			}
		} else {
			if (getCookie("FAQ_QQGROUP_HIT_" + id) != null) {
				return;
			}
		}

		var p = {};
		p.id = id;
		$.ajax({
			url : _FAQ_HIT_URL,
			data : p,
			dataType : 'json',
			success : function(data) {
				if (data /* <![CDATA[ */&& /* ]]> */data.code == 0) {
					if (window.sessionStorage) {
						window.sessionStorage.setItem("FAQ_QQGROUP_HIT_" + id,
								"true");
					} else {
						document.cookie = "FAQ_QQGROUP_HIT_" + id + "=true";
					}
				}
			},
			error : function() {
			},
			complete : function() {
			}
		});
	}
	/**
	 * 展示FAQ框
	 */
	function showFAQ() {
		$faqWin.animate({
			'right' : '5px',
			'z-index' : '999'
		}, 300);

		clearTimeout(hoverFunc);

		hoverFunc = setTimeout(function() {
			$faqWin.animate({
				'right' : '-360px',
				'z-index' : '777'
			}, 300, function() {
			});
		}, 5000);

	}

	var $faqWin = $(".faqWin");
	var $icon = $('<div class="faqbutton"></div>');
	var hoverFunc = undefined;

	$faqWin.bind('mouseover', function() {
		if (hoverFunc)
			clearTimeout(hoverFunc);
	});

	$faqWin.bind('mouseleave', function() {
		$faqWin.animate({
			'right' : '-360px',
			'z-index' : '777'
		}, 300, function() {
		});
	});

	$icon.bind('click', function() {
		$icon.blur();

		getFAQ();
	});

	$("body").append($icon);
});